package br.jcrsistemas.componentes;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class SysDocument extends PlainDocument{

	/**
	 * 
	 */
	private static final long serialVersionUID = 4886438988274067049L;
	
	private int maxLength = 0;
	private boolean upperCase = true;
	private boolean apenasNumeros = false;
	
	@Override
	public void insertString(int offs, String str, AttributeSet a)
			throws BadLocationException {
		if(isApenasNumeros()) {
			for (int i = 0; i < str.length(); i++) {
				if (Character.isDigit(str.charAt(i)) == false) {
					return;
				}
			}
		}
		
    	if(getLength() + str.length() > maxLength && maxLength > 0 ) {
    		return;
    	}
        if (str == null)
            return;
        if(upperCase) {
        	super.insertString(offs, str.toUpperCase(), a);
        	return;
        }
		super.insertString(offs, str, a);
	}
	
	public void setMaxLength(int maxLength) {
		this.maxLength = maxLength;
	}
	public int getMaxLength() {
		return maxLength;
	}
	public void setUpperCase(boolean upperCase) {
		this.upperCase = upperCase;
	}
	public boolean isUpperCase() {
		return upperCase;
	}

	public void setApenasNumeros(boolean apenasNumeros) {
		this.apenasNumeros = apenasNumeros;
	}

	public boolean isApenasNumeros() {
		return apenasNumeros;
	}

}
